class Solution {
  public:
    /**
     *
     * @param head ListNode类
     * @param m int整型
     * @param n int整型
     * @return ListNode类
     */

    // 头插法迭代
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        ListNode* dummy = new ListNode(0);  // 虚拟头节点
        dummy->next = head;
        ListNode* pre = dummy, *cur = head;
        for(int i = 1; i < m; ++i){  // 找到m节点
            pre = cur;
            cur = cur->next;
        }
        // 此时cur是m节点，进行翻转(头插法)
        for(int i = m; i < n; ++i){
            ListNode* next = cur->next;
            cur->next = next->next;
            next->next = pre->next;
            pre->next = next;
        }
        return dummy->next;
    }
};